package cn.donting.jme.edit.ex;


import java.util.*;


/**
 * 编辑器扩展资源 加载器与注册器
 * @author donting
 */
public class ExResourcesLoad {

    /**
     * 扩展加载器
     */
    private static List<IconResourcesLoader> iconResourcesLoaderList = new ArrayList<>();
    private static List<InspectorUILoader> inspectorUILoaders = new ArrayList<>();
    private static List<ToolBarUILoader> toolBarUILoaders = new ArrayList<>();
    private static List<AttributeLoader> attributeLoaders = new ArrayList<>();
    private static List<AutoAttributeLoader> autoAttributeLoaders = new ArrayList<>();

    /**
     * spi 注册
     */
    static {
        register(ExResourcesLoad.class.getClassLoader());
    }

    /**
     * 注册扩展器
     * @param classLoader 类加载器
     */
    public static void register(ClassLoader classLoader) {
        if (classLoader == null) {
            classLoader = ExResourcesLoad.class.getClassLoader();
        }
        register(iconResourcesLoaderList, IconResourcesLoader.class, classLoader);
        register(inspectorUILoaders, InspectorUILoader.class, classLoader);
        register(toolBarUILoaders, ToolBarUILoader.class, classLoader);
        register(attributeLoaders, AttributeLoader.class, classLoader);
        register(autoAttributeLoaders, AutoAttributeLoader.class, classLoader);
    }

    /**
     * 注册加载器
     * @param store  存储列表
     * @param tClass  对应加载器的 Class
     * @param classLoader 类加载器
     * @param <T>
     */
    public static <T> void register(List<T> store, Class<T> tClass, ClassLoader classLoader) {
        ServiceLoader<T> load = ServiceLoader.load(tClass, classLoader);
        Iterator<T> iterator = load.iterator();
        while (iterator.hasNext()) {
            T next = iterator.next();
            if (next.getClass().getClassLoader().equals(classLoader)) {
                store.add(next);
            }
        }
    }

    /**
     * 获取加载器的 第一个
     * @param tClass 加载器类型
     * @param <T> 实例T
     * @return
     */
    public static <T> T getFirstResources(Class<T> tClass) {
        if (IconResourcesLoader.class.equals(tClass)) {
            return (T) iconResourcesLoaderList.get(0);
        }
        return null;
    }

    /**
     * 获取InspectorUILoader
     * @return
     */
    public static List<InspectorUILoader> getInspectorUILoader() {
        List<InspectorUILoader> list = (List<InspectorUILoader>) Collections.unmodifiableList(inspectorUILoaders);
        return list;
    }

    /**
     * 获取ToolBarUILoader
     * @return
     */
    public static List<ToolBarUILoader> getToolBarUILoader() {
        List<ToolBarUILoader> list = (List<ToolBarUILoader>) Collections.unmodifiableList(toolBarUILoaders);
        return list;
    }

    /**
     * 获取 AttributeLoader
     * @return
     */
    private static List<AttributeLoader> getAttributeLoaders() {
        List<AttributeLoader> list = (List<AttributeLoader>) Collections.unmodifiableList(attributeLoaders);
        return list;
    }
  /**
     * 获取 AttributeLoader
     * @return
     */
    public static List<AutoAttributeLoader> getAutoAttributeLoaders() {
        List<AutoAttributeLoader> list = Collections.unmodifiableList(autoAttributeLoaders);
        return list;
    }

    /**
     * 获取能够加载 Object 的AttributeLoader
     *
     * @param o
     * @return
     */
    public static List<AttributeLoader> getAttributeLoaders(Object o) {
        List<AttributeLoader> attributeLoaders = ExResourcesLoad.getAttributeLoaders();
        List<AttributeLoader> list = new ArrayList<>();
        for (AttributeLoader attributeLoader : attributeLoaders) {
            if (attributeLoader.isLoad(o)) {
                list.add(attributeLoader);
            }
        }
        return list;
    }





}
